<?php
$this->load->view('header');

if (strlen($this->session->flashdata('Error')) > 0) {
    echo '<div class="alert alert-danger alert-dismissable">';
    echo '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' . $this->session->flashdata('Error') . '</div>';
    $this->session->unset_userdata('Error');
} elseif (strlen($this->session->flashdata('Success')) > 0) {
    echo '<div class="alert alert-success alert-dismissable">';
    echo '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' . $this->session->flashdata('Success') . '</div>';
    $this->session->unset_userdata('Success');
}
?>

<script type="text/javascript">

    $(document).ready(function() {
        $('#search_by_area').select2({
            placeholder: "Select Area",
            allowClear: true
        });
        $('#search_by_regional').select2({
            placeholder: "Select Regional",
            allowClear: true
        });
//        $('#search_by_status').select2({
//            placeholder: "Select Status",
//            allowClear: true
//        });
    });

    function search_data() {
        $.post("<?= site_url() ?>setup/search_courier", {
            search_by_courier: $('#search_by_courier').val(),
            search_by_area: $('#search_by_area').val(),
            search_by_regional: $('#search_by_regional').val(),
            search_by_status: $('#search_by_status').val()
        },
        function(data) {
            $('#divload').html(data);
        });
    }

    function edit_data(id) {
        $.post("<?= site_url() ?>setup/edit_courier", {price_id: id}, function(data) {
            $('#loader').html(data);
        });
    }

    function close_popup() {
        $('#dialog').dialog('close');
    }

    function load_page_courier(page, ipp) {
        $.post("<?= site_url() ?>setup/load_page_courier", {
            page: page,
            ipp: ipp,
            search_by_courier: $('#search_by_courier').val(),
            search_by_area: $('#search_by_area').val(),
            search_by_regional: $('#search_by_regional').val(),
            search_by_status: $('#search_by_status').val()
        },
        function(data) {
            $('#divload').html(data);
        });
    }

    function activation(id, cond) {
        var page = $('#page_val').val();
        var ipp = $('#ipp_val').val();
        var str = 'Are you sure to deactivate this courier?';
        if (cond != 'Y') {
            str = 'Are you sure to activate this courier?';
        }
        var cek = confirm(str);
        if (cek) {
            $.post("<?= site_url() ?>setup/activation_courier", {
                id: id,
                cond: cond
            },
            function(data) {
                load_page_courier(page, ipp);
            });
        }
    }

    function add_data() {
        $.post("<?= site_url() ?>setup/add_courier", function(data) {
            $('#loader').html(data);
        });
    }

    function changearea(area) {
        var areaname = area;
        if (areaname !== null) {
            $.post("<?= site_url() ?>setup/get_regional", {
                area: areaname
            },
            function(data) {
                $('#search_by_regional').html(data);
            });
        }
    }
</script>
<!--div style="margin-top:20px;"></div-->

<div id="loader" class="container">
    <form class="form-horizontal" id="access" role="form" method="post" action="#">
        <div class="col-md-offset-1">
            <div class="col-md-11">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h4 class="panel-title"><b class="caret"></b><b> Search Courier</b></h4>
                    </div>
                    <div class="panel-body">	
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="col-md-5"><p class="text-left">Courier Name</p></label>
                                <div class="col-md-7"><input type="text" class="form-control input-sm" id="search_by_courier" name="search_by_courier"></div>
                            </div>
                            <div class="form-group">
                                <label class="col-md-5"><p class="text-left">Status</p></label>
                                <div class="col-md-7">
                                    <select name="search_by_status" id="search_by_status" class="form-control input-sm">
                                        <option value="">-- select --</option>
                                        <option value="Y">Active</option>
                                        <option value="N">Non Active</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6 col-md-5">
                            <div class="form-group">
                                <label class="col-md-4"><p class="text-left">Area</p></label>
                                <div class="col-md-8">
                                    <select name="search_by_area" id="search_by_area" class="form-control input-sm select2" onchange="changearea(this.value);">
                                        <option value="">-- select --</option>
                                        <?php
                                        if (isset($list_area) && !empty($list_area)):
                                            foreach ($list_area as $row_area):
                                                echo '<option value="' . trim(strtolower($row_area['AREA_NAME'])) . '">' . trim(strtoupper($row_area['AREA_NAME'])) . '</option>';
                                            endforeach;
                                        endif;
                                        ?>
                                    </select>
                                </div>
                            </div>						
                            <div class="form-group">
                                <label class="col-md-4"><p class="text-left">Regional</p></label>
                                <div class="col-md-8">
                                    <select name="search_by_regional" id="search_by_regional" class="form-control input-sm">
                                        <option disabled value="">Select Area First</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <div class="col-md-5"><label for="button" class="control-label">&nbsp;</label></div>
                                <div class="col-md-4">
                                    <button type="button" class="btn btn-primary btn-block" onclick="search_data();">Search</button>
                                </div>
                                <div class="col-md-3">
                                    <button type="reset" class="btn btn-default btn-block">Clear</button>
                                </div>
                            </div>
                        </div>


                    </div>
                </div>

                <div class="panel-body well" id="divload">
                    <div class="col-md-6">
                        <div class="form-group">
                            <div class="col-md-4" style="padding-bottom: 0px;">
                                <button type="button" class="btn btn-success btn-block" onclick="add_data();">Add Courier</button>
                            </div>
                        </div>
                    </div>
                    <div id="tablecontent" class="col-md-12">						
                        <div class="form-group">
                            <div class="table-responsive">
                                <table class="table table-bordered table-condensed table-striped" style="background:#f9f9f9;">
                                    <thead>
                                        <tr>
                                            <th class="tblhead" style="width:5%;text-align:center;">No</th>
                                            <th class="tblhead" style="width:25%;text-align:center;">Courier Name</th>
                                            <th class="tblhead" style="width:20%;text-align:center;">Area / Regional</th>
                                            <th class="tblhead" style="width:10%;text-align:center;">Rate < 1Kg</th>
                                            <th class="tblhead" style="width:10%;text-align:center;">Rate Middle</th>
                                            <th class="tblhead" style="width:10%;text-align:center;">Rate > 5Kg</th>
                                            <th class="tblhead" style="width:10%;text-align:center;">Status</th>
                                            <th class="tblhead" style="width:10%;text-align:center;">Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        if (empty($list_data)) {
                                            ?>
                                            <tr><td colspan="8" style="text-align:center">No Data Available</td></tr>
                                            <?php
                                        } else {
                                            foreach ($list_data as $row) {
                                                ?>
                                                <tr>
                                                    <?php
                                                    $courier_name = $row["COURIER_NAME"];
                                                    $area_name = $row["AREA_NAME"];
                                                    $regional_name = $row["REGIONAL_NAME"];
                                                    $courier_id = $row["COURIER_ID"];
                                                    $price_id = $row["PRICE_ID"];
                                                    $a1kg = $row["PRICE_1KG"];
                                                    $a15kg = $row["PRICE_15KG"];
                                                    $a5kg = $row["PRICE_5KG"];
                                                    $price_status = $row["PRICE_STATUS"];

                                                    $courier_status = ($price_status == 'Y') ? 'Active' : 'Not Active';
                                                    ?>  

                                                    <td style="text-align:center;"><?php echo $row["RECNUM"]; ?>.</td>
                                                    <td style="text-align:left;"><?php echo ucfirst($courier_name); ?></td>
                                                    <td style="text-align:left;"><?php echo ucfirst($area_name) . ' / ' . ucfirst($regional_name); ?></td>
                                                    <td style="text-align:right;"><?php echo $a1kg; ?> /Kg</td>
                                                    <td style="text-align:right;"><?php echo $a15kg; ?> /Kg</td>
                                                    <td style="text-align:right;"><?php echo $a5kg; ?> /Kg</td>
                                                    <td style="text-align:center;">
                                                        <?php if ($price_status == 'Y') : ?>
                                                            <a class="btn btn-sm btn-primary" href="#" onclick="activation('<?php echo $price_id ?>', '<?php echo $price_status ?>');">
                                                                <?php echo $courier_status; ?></a>
                                                        <?php else: ?>
                                                            <a class="btn btn-sm btn-danger" href="#" onclick="activation('<?php echo $price_id ?>', '<?php echo $price_status ?>');">
                                                                <?php echo $courier_status; ?></a>
                                                        <?php endif; ?>
                                                    </td>
                                                    <td style="text-align:center;">
                                                        <a class="btn btn-sm btn-warning" href="#" onclick="edit_data('<?php echo $price_id ?>');">
                                                            Edit</a>
                                                    </td>
                                                </tr>
                                                <?php
                                            }
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <div class="form-group">
                            <div id="mypaging">
                                <?php
                                echo $paging;
                                echo $jump_page;
                                echo $ipp;
                                ?>
                            </div>								
                        </div>						
                    </div>
                </div>
            </div>
        </div>
    </form></div>
<!--div style="margin-top:20px;"></div-->

<?php $this->load->view('footer'); ?>

